package com.chuangshi.core.vote.playervote.controller.admin;

import com.chuangshi.core.kit.Controller;
import com.chuangshi.core.kit.IdWorker;
import com.chuangshi.core.kit.Result;
import com.chuangshi.core.vote.playervote.entity.PlayerVote;
import com.chuangshi.core.vote.playervote.controller.admin.request.*;
import com.chuangshi.core.vote.playervote.controller.admin.response.*;
import com.chuangshi.core.vote.playervote.entity.column.PlayerVoteColumn;
import com.chuangshi.core.vote.playervote.service.PlayerVoteService;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投票记录管理端控制器
 *
 * @author ggliming
 * @date 2019-02-21
 */
@Api(tags = "投票记录管理端接口管理")
@RestController
public class PlayerVoteAdminController extends Controller {

    @Autowired
    private PlayerVoteService playerVoteService;

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "获取投票记录列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerVoteAdminV1ListRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = PlayerVoteAdminV1ListResponse.class)
    })
    @RequestMapping(value = "/vote/player/vote/admin/v1/list", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1List(@ApiIgnore @RequestBody PlayerVoteAdminV1ListRequest body) {
        validateRequest(body);

        Result result = playerVoteService.listForAdmin(body);

        validateResponse(PlayerVoteAdminV1ListResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "获取单条投票记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerVoteAdminV1FindRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回参数", response = PlayerVoteAdminV1FindResponse.class)
    })
    @RequestMapping(value = "/vote/player/vote/admin/v1/find", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Find(@ApiIgnore @RequestBody PlayerVoteAdminV1FindRequest body) {
        validateRequest(body);

        PlayerVote result = playerVoteService.find(body.getPlayerVoteId());

        validateResponse(PlayerVoteAdminV1FindResponse.class);

        return renderJson(result);
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "新增投票记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerVoteAdminV1SaveRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功")
    })
    @RequestMapping(value = "/vote/player/vote/admin/v1/save", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Save(@ApiIgnore @RequestBody PlayerVoteAdminV1SaveRequest body) {
        validateRequest(body);

        PlayerVote playerVote = new PlayerVote(body);

        String playerVoteId = IdWorker.getIdStr();
        playerVote.setPlayerVoteId(playerVoteId);

        Boolean result = playerVoteService.save(playerVote);

        return renderJson(result, result ? "新增成功" : "新增不成功");
    }

    @ApiOperationSort(value = 40)
    @ApiOperation(value = "修改投票记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerVoteAdminV1UpdateRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功")
    })
    @RequestMapping(value = "/vote/player/vote/admin/v1/update", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Update(@ApiIgnore @RequestBody PlayerVoteAdminV1UpdateRequest body) {
        validateRequest(body);

        PlayerVote playerVote = new PlayerVote(body);

        Boolean result = playerVoteService.update(playerVote, body.getSystemVersion());

        return renderJson(result, result ? "修改成功" : "修改不成功");
    }

    @ApiOperationSort(value = 50)
    @ApiOperation(value = "删除投票记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "请求参数", required = true, paramType = "body", dataType = "PlayerVoteAdminV1DeleteRequest")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功")
    })
    @RequestMapping(value = "/vote/player/vote/admin/v1/delete", method = {RequestMethod.POST}, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> v1Delete(@ApiIgnore @RequestBody PlayerVoteAdminV1DeleteRequest body) {
        validateRequest(body);

        Boolean result = playerVoteService.delete(body.getPlayerVoteIdList());

        return renderJson(result, result ? "删除成功" : "删除不成功");
    }

}